package com.hellomp;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class testMinio {
    public static void main(String[] args) {

        String endpoint = "http://192.168.10.11:9000";
        String accessKey = "minioadmin";
        String secretKey = "minioadmin";
        String bucketName = "hello-minio";


        MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();

        try {
            boolean buketExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if(!buketExist) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());

                String policy = """
                        {
                               "Statement" : [ {
                                 "Action" : "s3:GetObject",
                                 "Effect" : "Allow",
                                 "Principal" : "*",
                                 "Resource" : "arn:aws:s3:::%s/*"
                               } ],
                               "Version" : "2012-10-17"
                        }
                        """.formatted(bucketName);

                minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build());

            }
            minioClient.uploadObject(UploadObjectArgs.builder().filename("D:\\javaweb\\springboot-project\\7.images\\房间-厨房-2.jpg").bucket(bucketName).object("房间-厨房-2.jpg").build());
            System.out.println("文件上传成功");


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
